<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="料架号" prop="materialRackNo">
        <el-input
          v-model="queryParams.materialRackNo"
          placeholder="请输入料架号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码门编号" prop="doorCode">
        <el-input
          v-model="queryParams.doorCode"
          placeholder="请输入扫码门编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AGV编号" prop="agvCode">
        <el-input
          v-model="queryParams.agvCode"
          placeholder="请输入AGV编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择收货状态" clearable>
          <el-option
            v-for="dict in dict.type.stack_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stack:stackTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stack:stackTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stackTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务号" align="center" prop="taskCode" width="200"/>
      <el-table-column label="料架号" align="center" prop="materialRackNo" />
      <el-table-column label="扫码门编号" align="center" prop="doorCode" />
      <el-table-column label="AGV编号" align="center" prop="agvCode" />
      <el-table-column label="计划收货数" align="center" prop="planCount" />
      <el-table-column label="扫码门收货数" align="center" prop="realityCount" />
      <el-table-column label="PAD收货数" align="center" prop="padCount" />
      <!-- <el-table-column label="扫描率" align="center" prop="ratio" /> -->
      <el-table-column label="收货状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stack_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180" />
      <el-table-column label="收货完成时间" align="center" prop="finishTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['stack:stackTask:edit']"
          >任务明细</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stack:stackTask:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改托盘收货任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务号" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入任务号" />
        </el-form-item>
        <el-form-item label="料架号" prop="materialRackNo">
          <el-input v-model="form.materialRackNo" placeholder="请输入料架号" />
        </el-form-item>
        <el-form-item label="扫码门编号" prop="doorCode">
          <el-input v-model="form.doorCode" placeholder="请输入扫码门编号" />
        </el-form-item>
        <el-form-item label="AGV编号" prop="agvCode">
          <el-input v-model="form.agvCode" placeholder="请输入AGV编号" />
        </el-form-item>
        <el-form-item label="计划收货数" prop="planCount">
          <el-input v-model="form.planCount" placeholder="请输入计划收货数" />
        </el-form-item>
        <el-form-item label="实际收货数" prop="realityCount">
          <el-input v-model="form.realityCount" placeholder="请输入实际收货数" />
        </el-form-item>
        <el-form-item label="扫描率" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入扫描率" />
        </el-form-item>
        <el-form-item label="收货状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择收货状态">
            <el-option
              v-for="dict in dict.type.stack_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货完成时间" prop="finishTime">
          <el-date-picker clearable
            v-model="form.finishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收货完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <div :class="['detail-popup',showDetail?'show':'']">
      <div class="popup-mask" @click="showDetail=false"></div>
      <div :class="['popup-main']">
        <el-tabs v-model="tabName" @tab-click="handleTabs">
          <el-tab-pane label="收货标签" name="taskCodeList">
            <el-table  height="calc(100vh - 110px)" :data="stackTaskDetail.taskCodeList" style="width: 100%;">
              <el-table-column label="标签编号" prop="code" align="center" />
              <el-table-column label="状态"  prop="status"  align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.code_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="第一次识别标签" name="firstVisualReceipt" v-if="stackTaskDetail.firstVisualReceipt">
            <el-table  height="calc(100vh - 110px)" :data="stackTaskDetail.firstVisualReceipt.taskDetailCodeList" style="width: 100%;">
              <el-table-column label="标签编号" prop="code" align="center" />
              <el-table-column label="状态"  prop="status"  align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.code_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
              <el-table-column label="是否有效"  prop="state"  align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.code_state" :value="scope.row.state"/>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="第一次识别照片" name="firstVisualImages" v-if="stackTaskDetail.firstVisualReceipt && stackTaskDetail.firstVisualReceipt.visualReceiptImageList">
            <div class="demo-image__placeholder" style="height:calc(100vh - 110px);overflow-y: auto;">
              <div class="block" v-for="item in stackTaskDetail.firstVisualReceipt.visualReceiptImageList" :key="item">
                <el-image :src="item"></el-image>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="第二次识别标签" name="secondVisualReceipt" v-if="stackTaskDetail.secondVisualReceipt">
            <el-table  height="calc(100vh - 110px)" :data="stackTaskDetail.secondVisualReceipt.taskDetailCodeList" style="width: 100%;">
              <el-table-column label="标签编号" prop="code" align="center" />
              <el-table-column label="状态"  prop="status"  align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.code_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </el-table>            
          </el-tab-pane>
          <el-tab-pane label="第二次识别照片" name="firstVisualImages" v-if="stackTaskDetail.secondVisualReceipt && stackTaskDetail.secondVisualReceipt.visualReceiptImageList">
            <div class="demo-image__placeholder" style="height:calc(100vh - 110px);overflow-y: auto;">
              <div class="block" v-for="item in stackTaskDetail.secondVisualReceipt.visualReceiptImageList" :key="item">
                <el-image :src="item"></el-image>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="PAD扫描标签" name="padCodeList">
            <el-table  height="calc(100vh - 110px)" :data="stackTaskDetail.padCodeList" style="width: 100%;">
              <el-table-column label="标签编号" prop="code" align="center" />
              <el-table-column label="状态"  prop="status"  align="center">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.code_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </el-table>            
          </el-tab-pane>
          <el-table-column label="是否有效"  prop="state"  align="center">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.code_state" :value="scope.row.state"/>
            </template>
          </el-table-column>
          <el-tab-pane label="调度日志" name="processTaskLog">
            <el-table height="calc(100vh - 110px)" v-loading="loading" :data="stackTaskDetail.taskProcesses" @selection-change="handleSelectionChange">
              <el-table-column type="expand">
                <template slot-scope="props">
                  <el-form label-position="left" class="demo-table-expand">
                    <el-form-item label="任务编号">
                      <span>{{ props.row.taskCode }}</span>
                    </el-form-item>
                    <el-form-item label="接受内容">
                      <span>{{ props.row.receiveContent }}</span>
                    </el-form-item>
                    <el-form-item label="应答内容">
                      <span>{{ props.row.respContent }}</span>
                    </el-form-item>
                    <el-form-item label="发送内容">
                      <span>{{ props.row.sendContent }}</span>
                    </el-form-item>
                    <el-form-item label="任务环节">
                      <dict-tag :options="dict.type.task_process_link_name" :value="props.row.taskLinkName"/>
                    </el-form-item>
                    <el-form-item label="开始时间">
                      <span>{{ props.row.taskLinkStartTime }}</span>
                    </el-form-item>
                    <el-form-item label="完成时间">
                      <span>{{ props.row.taskLinkFinishTime }}</span>
                    </el-form-item>
                    <el-form-item label="处理状态">
                      <dict-tag :options="dict.type.process_status" :value="props.row.status"/>
                    </el-form-item>
                  </el-form>
                </template>
              </el-table-column>
              <el-table-column label="任务编号" align="center" prop="taskCode" width="200" />
              <el-table-column label="接收内容" align="center" :show-overflow-tooltip="true"  prop="receiveContent"/>
              <el-table-column label="应答内容" align="center" :show-overflow-tooltip="true"  prop="respContent" />
              <el-table-column label="发送内容" align="center" :show-overflow-tooltip="true"  prop="sendContent" />
              <el-table-column label="任务环节" align="center" prop="taskLinkName">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.task_process_link_name" :value="scope.row.taskLinkName"/>
                </template>
              </el-table-column>
              <el-table-column label="开始时间" align="center" prop="taskLinkStartTime" width="180" />
              <el-table-column label="完成时间" align="center" prop="taskLinkFinishTime" width="180" />
              <el-table-column label="处理状态" align="center" prop="status">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.process_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-view"
                    v-if="scope.row.status===500"
                    @click="handleRetryCallback(scope.row.id)"
                  >重试</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
        
      </div>
    </div>
  </div>
</template>

<script>
import { listStackTask, getStackTask, delStackTask, addStackTask, updateStackTask } from "@/api/stack/stackTask";
import { retryCallback } from "@/api/task/taskProcess";
export default {
  name: "StackTask",
  dicts: ['stack_status','code_status','code_state','task_process_link_name','process_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      tabName:'taskCodeList',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 托盘收货任务表格数据
      stackTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        materialRackNo: null,
        doorCode: null,
        agvCode: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "收货状态不能为空", trigger: "change" }
        ],
      },
      showDetail:false,
      stackTask:[],
      stackTaskDetail:{
        taskCodeList:[],
        padCodeList:[],
        taskProcesses:[],
        firstVisualReceipt:{
          taskDetailCodeList:[]
        },
        secondVisualReceipt:{
          taskDetailCodeList:[]
        },
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询托盘收货任务列表 */
    getList() {
      this.loading = true;
      listStackTask(this.queryParams).then(response => {
        this.stackTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskCode: null,
        materialRackNo: null,
        doorCode: null,
        agvCode: null,
        planCount: null,
        realityCount: null,
        ratio: null,
        status: null,
        startTime: null,
        finishTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加托盘收货任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStackTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改托盘收货任务";
      });
    },
    //重试
    handleRetryCallback(id){
      retryCallback(id).then( res => {
        if(res.success) this.$modal.msgSuccess("操作成功");
        else this.$modal.msgError("操作失败");
        this.getList()
      })
    },
    handleView(row){
      const id = row.id;
      getStackTask(id).then(response => {
        this.stackTaskDetail = response.data;
        this.stackTask = response.data.taskCodeList;
        this.showDetail = true;
      });
    },
    handleTabs(tabName){
      
    },
    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateStackTask(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addStackTask(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除托盘收货任务编号为"' + ids + '"的数据项？').then(function() {
        return delStackTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stack/stackTask/export', {
        ...this.queryParams
      }, `stackTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style scoped lang=scss>
.detail-popup{
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  .popup-mask{
    position: fixed;
    top: 0;
    left: 0;
    opacity: 0;
    -webkit-transition: opacity 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
    transition: opacity 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
    background: rgba(0, 0, 0, 0.2);
    z-index: -1;
  }
  .popup-main{
    padding:50px 20px;
    width: 100%;
    max-width: 1200px;
    height: 100vh;
    position: fixed;
    top: 0;
    right: 0;
    -webkit-box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.05);
    box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.05);
    -webkit-transition: all 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
    transition: all 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
    background: #fff;
    z-index: 40000;
    transform: translate(100%);
    overflow: hidden;
  }
  &.show{
    transition: all 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
    .popup-mask{
      z-index: 20000;
      opacity: 1;
      width: 100%;
      height: 100%;
    }
    .popup-main{
      -webkit-transform: translate(0);
      transform: translate(0);
    }
  }
}
</style>